var applyTypeValue = "";
var overallType = "";

var examineeType = "";
var applyType = "";
var testType = "";
var volunteerOne = "";
var volunteerTwo = "";
var volunteerThree = "";
var volunteerFour = "";
var volunteerFive = "";

var nowSelectValue = "";

var payType = "";
$(document).ready(function(){
	
	$.ajax({
		type:'GET',
		cache:false,
		url:rootPath+"/getOrdersByStudentId",
		async:false,
		data:{
			StudentId:student_id
		},
		success:function(result)
		{
			payType = result.extend.code;
		}
	});
	
	$.ajax({
		type:'GET',
		cache:false,
		url:rootPath+"/getStudentById",
		async:false,
		data:{
			StudentId:student_id
		},
		success:function(result)
		{
			var promiseType = result.extend.student.promiseType;
			if(promiseType==1 && payType==1)
				window.location.href = "affirmMess.html";
			
			var cardId = result.extend.student.cardId;
			var graduationType = result.extend.student.graduationType;
			var examineeType = result.extend.student.examineeType;
			
			document.getElementById("cardId").innerHTML = cardId;
			switch (graduationType)
			{
				case 1:
					document.getElementById("graduationType").innerHTML = '高中';
					break;
				case 2:
					document.getElementById("graduationType").innerHTML = '中等师范';
					break;
				case 3:
					document.getElementById("graduationType").innerHTML = '中等专业学校';
					break;
				case 4:
					document.getElementById("graduationType").innerHTML = '职业高中学校';
					break;
				case 5:
					document.getElementById("graduationType").innerHTML = '技工学校';
					break;
			}
			switch (examineeType)
			{
				case 1:
					document.getElementById("examineeType").innerHTML = '城市应届';
					break;
				case 2:
					document.getElementById("examineeType").innerHTML = '农村应届';
					break;
				case 3:
					document.getElementById("examineeType").innerHTML = '城市往届';
					break;
				case 4:
					document.getElementById("examineeType").innerHTML = '农村往届';
					break;
			}
			
			examineeType = result.extend.student.examineeType;
			applyType = result.extend.student.applyType;
			testType = result.extend.student.testType;
			volunteerOne = result.extend.student.volunteerOne;
			volunteerTwo = result.extend.student.volunteerTwo;
			volunteerThree = result.extend.student.volunteerThree;
			volunteerFour = result.extend.student.volunteerFour;
			volunteerFive = result.extend.student.volunteerFive;
			
			switch(applyType)
			{
				case 1:document.getElementById("applyType").innerHTML = '文史';break;
				case 2:document.getElementById("applyType").innerHTML = '理工';break;
			}
			applyTypeValue = applyType;
			overallType = testType;
			
			if(testType == "1"){
				$('.a1').attr('checked',true);
				var volunteerSelect = "<div class='b01'><span class='t4'>第一志愿：<select  class='x1' onchange='changeVolunteerSelect(1)' id='volunteerOneSelect'><option value = 'null'>----请选择----</option></select></div>";
				$("#volunteer").html(volunteerSelect);
				changeVolunteerSelect(0);
				$("#volunteerOneSelect").val(nowSelectValue);
			}else if(testType == "2"){
				$('.a2').attr('checked',true);
				var volunteerSelect = "<div class='b01'><span class='t4'>第一志愿：<select  class='x1' onchange='changeVolunteerSelect(1)' id='volunteerOneSelect'><option value = 'null'>----请选择----</option></select></div>";
				
				volunteerSelect += "<div class='b01'><span class='t4'>第二志愿：<select  class='x1' onchange='changeVolunteerSelect(2)' id='volunteerTwoSelect'><option value = 'null'>----请选择----</option><option value = 'wu'>无</option></select></div>";
				volunteerSelect += "<div class='b01'><span class='t4'>第三志愿：<select  class='x1' onchange='changeVolunteerSelect(3)' id='volunteerThreeSelect'><option value = 'null'>----请选择----</option><option value = 'wu'>无</option></select></div>";
				volunteerSelect += "<div class='b01'><span class='t4'>第四志愿：<select  class='x1' onchange='changeVolunteerSelect(4)' id='volunteerFourSelect'><option value = 'null'>----请选择----</option><option value = 'wu'>无</option></select></div>";
				volunteerSelect += "<div class='b01'><span class='t4'>第五志愿：<select  class='x1' id='volunteerFiveSelect'><option value = 'null'>----请选择----</option><option value = 'wu'>无</option></select></div>";
				$("#volunteer").html(volunteerSelect);
				//document.getElementById("volunteer").innerHTML = volunteerSelect;
				changeVolunteerSelect(0);
				$("#volunteerOneSelect").val(nowSelectValue);
				changeVolunteerSelect(1);
				$("#volunteerTwoSelect").val(nowSelectValue);
				changeVolunteerSelect(2);
				$("#volunteerThreeSelect").val(nowSelectValue);
				changeVolunteerSelect(3);
				$("#volunteerFourSelect").val(nowSelectValue);
				changeVolunteerSelect(4);
				$("#volunteerFiveSelect").val(nowSelectValue);
			}
		}
	});
	
	$('.testTypeRadio').on('change',function(){
		overallType = $(this).val();
		var volunteerSelect = "<div class='b01'><span class='t4'>第一志愿：<select  class='x1' onchange='changeVolunteerSelect(1)' id='volunteerOneSelect'><option value = 'null'>----请选择----</option></select></div>";
		
		if(overallType == 2)
		{
			volunteerSelect += "<div class='b01'><span class='t4'>第二志愿：<select  class='x1' onchange='changeVolunteerSelect(2)' id='volunteerTwoSelect'><option value = 'null'>----请选择----</option><option value = 'null'>无</option></select></div>";
			volunteerSelect += "<div class='b01'><span class='t4'>第三志愿：<select  class='x1' onchange='changeVolunteerSelect(3)' id='volunteerThreeSelect'><option value = 'null'>----请选择----</option><option value = 'null'>无</option></select></div>";
			volunteerSelect += "<div class='b01'><span class='t4'>第四志愿：<select  class='x1' onchange='changeVolunteerSelect(4)' id='volunteerFourSelect'><option value = 'null'>----请选择----</option><option value = 'null'>无</option></select></div>";
			volunteerSelect += "<div class='b01'><span class='t4'>第五志愿：<select  class='x1' id='volunteerFiveSelect'><option value = 'null'>----请选择----</option><option value = 'null'>无</option></select></div>";
		}
		$("#volunteer").html(volunteerSelect);
		//document.getElementById("volunteer").innerHTML = volunteerSelect;
		changeVolunteerSelect(0);
	});
});

function changeVolunteerSelect(volunteerSelectValue)
{
	var volunteerOneSelect = document.getElementById("volunteerOneSelect");
	var volunteerTwoSelect = document.getElementById("volunteerTwoSelect");
	var volunteerThreeSelect = document.getElementById("volunteerThreeSelect");
	var volunteerFourSelect = document.getElementById("volunteerFourSelect");
	var volunteerFiveSelect = document.getElementById("volunteerFiveSelect");
	
	var volunteerOneValue = "";
	var volunteerTwoValue = "";
	var volunteerThreeValue = "";
	var volunteerFourValue = "";
	for(var i=0;i<volunteerOneSelect.length;i++)
	{
		if(volunteerOneSelect[i].selected)
			volunteerOneValue = volunteerOneSelect[i].value;
	}
	if(overallType == 2)
	{
		for(var i=0;i<volunteerTwoSelect.length;i++)
		{
			if(volunteerTwoSelect[i].selected)
				volunteerTwoValue = volunteerTwoSelect[i].value;
		}
		for(var i=0;i<volunteerThreeSelect.length;i++)
		{
			if(volunteerThreeSelect[i].selected)
				volunteerThreeValue = volunteerThreeSelect[i].value;
		}
		for(var i=0;i<volunteerFourSelect.length;i++)
		{
			if(volunteerFourSelect[i].selected)
				volunteerFourValue = volunteerFourSelect[i].value;
		}
	}
	
	var ids = "";
	switch(volunteerSelectValue)
	{
		case 0:ids="%27%27";break;
		case 1:ids=volunteerOneValue;break;
		case 2:ids=volunteerOneValue+","+volunteerTwoValue;break;
		case 3:ids=volunteerOneValue+","+volunteerTwoValue+","+volunteerThreeValue;break;
		case 4:ids=volunteerOneValue+","+volunteerTwoValue+","+volunteerThreeValue+","+volunteerFourValue;break;
	}
	
	var publicVolunteerSelectOption="<option value = 'null'>----请选择----</option>";
	if(overallType == 2)
	{
		switch(volunteerSelectValue)
		{
			/*case 1:volunteerTwoSelect.innerHTML=publicVolunteerSelectOption;
					volunteerThreeSelect.innerHTML=publicVolunteerSelectOption;
					volunteerFourSelect.innerHTML=publicVolunteerSelectOption;
					volunteerFiveSelect.innerHTML=publicVolunteerSelectOption;break;
			case 2:volunteerThreeSelect.innerHTML=publicVolunteerSelectOption;
					volunteerFourSelect.innerHTML=publicVolunteerSelectOption;
					volunteerFiveSelect.innerHTML=publicVolunteerSelectOption;break;
			case 3:volunteerFourSelect.innerHTML=publicVolunteerSelectOption;
					volunteerFiveSelect.innerHTML=publicVolunteerSelectOption;break;
			case 4:volunteerFiveSelect.innerHTML=publicVolunteerSelectOption;break;*/
				
			case 1:$("#volunteerTwoSelect").html(publicVolunteerSelectOption);
					$("#volunteerThreeSelect").html(publicVolunteerSelectOption);
					$("#volunteerFourSelect").html(publicVolunteerSelectOption);
					$("#volunteerFiveSelect").html(publicVolunteerSelectOption);break;
			case 2:$("#volunteerThreeSelect").html(publicVolunteerSelectOption);
					$("#volunteerFourSelect").html(publicVolunteerSelectOption);
					$("#volunteerFiveSelect").html(publicVolunteerSelectOption);break;
			case 3:$("#volunteerFourSelect").html(publicVolunteerSelectOption);
					$("#volunteerFiveSelect").html(publicVolunteerSelectOption);break;
			case 4:$("#volunteerFiveSelect").html(publicVolunteerSelectOption);break;
		}
	}
	
	if( volunteerSelectValue ==0 || volunteerSelectValue == 1 || (volunteerSelectValue == 2 && volunteerTwoValue != 'wu') || (volunteerSelectValue == 3 && volunteerThreeValue != 'wu')  || (volunteerSelectValue == 4 && volunteerFourValue != 'wu'))
	{
		$.ajax({
			type:'GET',
			cache:false,
			url:rootPath+"/getArts",
			async:false,
			data:{
				section_type:applyTypeValue,
				overall_type:overallType,
				ids:ids
			},
			success:function(result)
			{
				if(overallType == 1 && volunteerSelectValue == 1)
					;
				else
				{
					var volunteerSelect;
					var volunteerValue;
					switch(volunteerSelectValue)
					{
						case 0:volunteerSelect = volunteerOneSelect;break;
						case 1:volunteerSelect = volunteerTwoSelect;break;
						case 2:volunteerSelect = volunteerThreeSelect;break;
						case 3:volunteerSelect = volunteerFourSelect;break;
						case 4:volunteerSelect = volunteerFiveSelect;break;
					}
					
					var volunteerSelectOption="";
					if(volunteerSelectValue == 0)
						volunteerSelectOption="<option value = 'null'>----请选择----</option>";
					else
						volunteerSelectOption="<option value = 'null'>----请选择----</option><option value = 'wu'>无</option>";
					var volunteer = result.extend.science;
					for(var i=0;i<volunteer.length;i++)
					{
						volunteerSelectOption += "<option value ="+volunteer[i].id+">"+volunteer[i].major+"</option>";
						switch(volunteerSelectValue)
						{
							case 0:if(volunteer[i].major === volunteerOne){
										nowSelectValue = volunteer[i].id;
									}
									if(volunteerOne === "无"){
										nowSelectValue = "wu"
									}break;
							case 1:if(volunteer[i].major === volunteerTwo){
										nowSelectValue = volunteer[i].id;
									}
									if(volunteerTwo === "无"){
										nowSelectValue = "wu"
									}break;
							case 2:if(volunteer[i].major === volunteerThree){
										nowSelectValue = volunteer[i].id;
									}
									if(volunteerThree === "无"){
										nowSelectValue = "wu"
									}break;
							case 3:if(volunteer[i].major === volunteerFour){
										nowSelectValue = volunteer[i].id;
									}
									if(volunteerFour === "无"){
										nowSelectValue = "wu"
									}break;
							case 4:if(volunteer[i].major === volunteerFive){
										nowSelectValue = volunteer[i].id;
									}
									if(volunteerFive === "无"){
										nowSelectValue = "wu"
									}break;
						}
					}
					$(volunteerSelect).html(volunteerSelectOption)
					//volunteerSelect.innerHTML=volunteerSelectOption;
				}
			}
		});
	}
	else
	{
		switch(volunteerSelectValue)
		{
			/*case 2:volunteerThreeSelect.innerHTML="<option value = 'null'>----请选择----</option><option value = 'null'>无</option>";break;
			case 3:volunteerFourSelect.innerHTML="<option value = 'null'>----请选择----</option><option value = 'null'>无</option>";break;
			case 4:volunteerFiveSelect.innerHTML="<option value = 'null'>----请选择----</option><option value = 'null'>无</option>";break;*/
			
			case 2:$("#volunteerThreeSelect").html("<option value = 'null'>----请选择----</option><option value = 'wu'>无</option>");break;
			case 3:$("#volunteerFourSelect").html("<option value = 'null'>----请选择----</option><option value = 'wu'>无</option>");break;
			case 4:$("#volunteerFiveSelect").html("<option value = 'null'>----请选择----</option><option value = 'wu'>无</option>");break;
		}
	}
	
}

function ok()
{
		if(overallType != "")
		{
			var volunteerOneSelect = document.getElementById("volunteerOneSelect");
			for(var i=0;i<volunteerOneSelect.length;i++)
			{
				if(volunteerOneSelect[i].selected)
					volunteerOne = volunteerOneSelect[i].text;
			}
			
			if(overallType == 2)
			{
				var volunteerTwoSelect = document.getElementById("volunteerTwoSelect");
				var volunteerThreeSelect = document.getElementById("volunteerThreeSelect");
				var volunteerFourSelect = document.getElementById("volunteerFourSelect");
				var volunteerFiveSelect = document.getElementById("volunteerFiveSelect");
				
				for(var i=0;i<volunteerTwoSelect.length;i++)
				{
					if(volunteerTwoSelect[i].selected)
						volunteerTwo = volunteerTwoSelect[i].text;
				}
				for(var i=0;i<volunteerThreeSelect.length;i++)
				{
					if(volunteerThreeSelect[i].selected)
						volunteerThree = volunteerThreeSelect[i].text;
				}
				for(var i=0;i<volunteerFourSelect.length;i++)
				{
					if(volunteerFourSelect[i].selected)
						volunteerFour = volunteerFourSelect[i].text;
				}
				for(var i=0;i<volunteerFiveSelect.length;i++)
				{
					if(volunteerFiveSelect[i].selected)
						volunteerFive = volunteerFiveSelect[i].text;
				}
			}
			if(volunteerOne != "----请选择----")
				if(overallType == 2)
					if(volunteerTwo != "----请选择----")
						if(volunteerThree != "----请选择----")
							if(volunteerFour != "----请选择----")
								if(volunteerFive != "----请选择----")
									passByValue(volunteerOne,volunteerTwo,volunteerThree,volunteerFour,volunteerFive);
								else
									alert("请填写第五志愿或选择无");
							else
								alert("请填写第四志愿或选择无");
						else
							alert("请填写第三志愿或选择无");
					else
						alert("请填写第二志愿或选择无");
				else
					passByValue(volunteerOne,'无','无','无','无');
			else
				alert("请填写第一志愿");
		}
		else
			alert("请选择报考类别");
}

function passByValue(volunteerOne,volunteerTwo,volunteerThree,volunteerFour,volunteerFive)
{
	$.ajax({
		type:'GET',
		cache:false,
		url:rootPath+"/updateStudent",
		async:true,
		data:{
			id:student_id,
			testType:overallType,
			volunteerOne:volunteerOne,
			volunteerTwo:volunteerTwo,
			volunteerThree:volunteerThree,
			volunteerFour:volunteerFour,
			volunteerFive:volunteerFive
		},
		success:function(result)
		{
			if(result.code == 100)
			{
				window.location.href = "siteMessFill.html";
			}
			else
			{
				alert(result.extend.code);
			}
		}
	});
}

function resetValue()
{
	var testTypeRadio = document.getElementsByName("testTypeRadio");
	for(var i=0;i<testTypeRadio.length;i++)
	{
		if(testTypeRadio[i].checked)
			testTypeRadio[i].checked = false;
	}
	
	overallType = "";
	document.getElementById("volunteer").innerHTML = "";
}

function toMessageMessFill()
{
	if(volunteerOne)
	{
		if(overallType == 2)
		{
			if(volunteerTwo)
				if(volunteerThree)
					if(volunteerFour)
						if(volunteerFive)
							window.location.href = "messageMessFill.html";
		}
		else
			window.location.href = "messageMessFill.html";
	}
	else
	{
		alert("请点击下一步");
	}
}

function toSiteMessFill()
{
	if(volunteerOne)
	{
		if(overallType == 2)
		{
			if(volunteerTwo)
				if(volunteerThree)
					if(volunteerFour)
						if(volunteerFive)
							window.location.href = "siteMessFill.html";
		}
		else
			window.location.href = "siteMessFill.html";
	}
	else
	{
		alert("请点击下一步");
	}
}